<?php

namespace app\api\controller;

use app\api\model\ShopOrder;
use app\api\service\OrderService;
use think\Controller;
use app\common\model\Order as OrderModel;

class Order extends Base
{
    protected $beforeActionList = [
        'checkToken'=>['except'=>'notify,notify_redirect,pay']
    ];

    //下单
    public function place()
    {
        $data = input('post.');
        $data['user_id'] = $this->userId;
        $data['openid'] = $this->openId;
        $res = (new OrderService())->place($data);
        return self::apiResp($res);
    }

    //订单列表
    public function list()
    {
        $page = input('page',1);
        $row = input('row',10);
        //查询条件
        $map = [];
        ($status = input('status', '0')) != 0 && $map['status'] = $status;
        //待发货/待收货
        if($status == 2)
            $map['status'] = ['in',[
                OrderModel::ORDER_WAIT_SEND,
                OrderModel::ORDER_WAIT_SIGN
            ]];
        //售后
        if($status == 5)
            $map['status'] = ['in',[
                OrderModel::ORDER_RETURN_REQUEST,
                OrderModel::ORDER_RETURN_OK,
                OrderModel::ORDER_RETURN_REFUSE
            ]];

        $map['user_id'] = $this->userId;
        $res = ShopOrder::getList($map,$page,$row);
        return self::apiResp($res);
    }

    //订单详情
    public function detail()
    {
        $orderNo = input('order_no');
        $res = ShopOrder::getByOrderNo($orderNo,$this->userId);
        return self::apiResp($res);
    }

    //删除未支付订单
    public function cancel()
    {
        $orderNo = input('order_no');
        $res = ShopOrder::cancelById($orderNo,$this->userId);
        return self::apiResp($res);
    }

    //订单签收
    public function sign()
    {
        $orderNo = input('order_no');
        $res = ShopOrder::signById($orderNo,$this->userId);
        return self::apiResp($res);
    }

    //订单售后
    public function refund()
    {
        $orderNo = input('order_no');
        $remarks = input('remarks');
        $res = ShopOrder::refundById($orderNo,$this->userId,$remarks);
        return self::apiResp($res);
    }

    //支付签名
    public function pay()
    {
        $wechatApp = new \app\mpbase\library\WechatApp();
        $res = $wechatApp->pay();
        return json($res);
    }

    //支付回调
    public function notify_redirect()
    {
        $wechatApp = new \app\mpbase\library\WechatApp();
        $wechatApp->payNotify();
    }

    //Xdebug断点调试 拼接ideKey
    public function notify()
    {
        $url = str_replace('notify','notify_redirect',request()->url(true))."?XDEBUG_SESSION_START=PHPSTORM";
        $xmlData = file_get_contents('php://input');
        $result = curl_post_raw($url,$xmlData);
        return $result;
    }
}
function curl_post_raw($url, $rawData)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}